/*
 * Copyright (C) 2011 Macambira Team
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.googlecode.control;

import com.googlecode.entity.Disciplina;
import com.googlecode.entity.Professor;
import com.googlecode.exception.ProfessorException;
import com.googlecode.exception.ProfessorNaoEncontradoException;
import com.googlecode.persistence.ProfessorDAO;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe responsavel por realizar o CRUD de professor
 * 
 * @version 0.2 22 set 2011
 * @author Italo Cesar
 */
public class ControlProfessor{

    private Map<String, Professor> mapProfessores;
    private List<Professor> listProfessores;    
    private ProfessorDAO dao;
    
    public ControlProfessor() {
        this.mapProfessores = new HashMap<String, Professor>();
        this.listProfessores = new LinkedList<Professor>();
        this.dao = new ProfessorDAO();
    }

    public void addProfessor(Professor p) throws SQLException {
        try {
            //        this.mapProfessores.put(p.getId(), p);
            //        this.listProfessores.add(p);
            dao.CadastrarProfessor(p);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public Professor getProfessorID(String id) throws SQLException {
        try {
            //        if (mapProfessores.get(id) != null) {
            //            return mapProfessores.get(id);
            //        }
            //        throw new ProfessorNaoEncontradoException("Professor nao encontrado");
            return dao.ConsultaProfessorId(id);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }
    

    public Professor getProfessorNome(String nome) throws SQLException {
        try {
            //        for (Professor p : listProfessores) {
            //            if (p.getNome().equalsIgnoreCase(nome)) {
            //                return p;
            //            }
            //        }
            //        throw new ProfessorNaoEncontradoException("Professor nao encontrado");
            return dao.ConsultaProfessorNome(nome);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }

    public void removeProfessorNome(String nome) throws SQLException {
        try {
            //        for (Professor p : listProfessores) {
            //            if (p.getNome().equalsIgnoreCase(nome)) {
            //                this.listProfessores.remove(p);
            //                return;
            //            }
            //        }
            //        throw new ProfessorNaoEncontradoException("");
            dao.RemoverProfessorNome(nome);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void removeProfessorID(String id) throws SQLException {
        try {
            //        for (Professor p : listProfessores) {
            //            if (p.getId().equalsIgnoreCase(id)) {
            //                this.listProfessores.remove(p);
            //                break;
            //            }
            //        }
            //        this.mapProfessores.remove(id);
            dao.ConsultaProfessorId(id);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public List<Professor> getListaProfessores() {
        return this.listProfessores;
    }

    public Map<String, Professor> getMapProfessores() {
        return this.mapProfessores;
    }

    public int listGetSize() {
        return this.listProfessores.size();
    }

    public int mapGetSize() {
        return this.mapProfessores.size();
    }
    
    public Professor getObjetoProfessor(Professor procurado) throws SQLException {
        try {
            //        Professor p = null;
            //        for (Professor p2 : this.listProfessores){
            //            if (p2.equals(procurado)){
            //               p = p2;
            //            }
            //        }
            return dao.ConsultaProfessorNome(procurado.getNome());
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }
    
    public void addProjetoProfessor(String nomeProf, String projeto) {
        
        for (Professor p : listProfessores) {
            if (p.getNome().equals(nomeProf)) {
                p.getListaProjetos().add(projeto);
                break;
            }
        }
        //dao.ConsultaProfessorNome(nomeProf).getListaProjetos().add(projeto);
    }
    
    public void addDisciplinasPreferenciais(String nomeProf, Disciplina disciplina){
        for (Professor p : listProfessores){
            if (p.getNome().equals(nomeProf)){
                p.getListDisciplinasPreferenciais().add(disciplina);
                break;
            }
        }
    }
    
}
